package com.tjsinfo.tjsoa.schedule.bo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.Dao.JDBCDao;
import com.tjsinfo.tjsoa.schedule.dao.IScheduleMainDAO;
import com.tjsinfo.tjsoa.schedule.vo.ScheduleMain;


public class ScheduleMainBo implements IScheduleMainBo {
	private IScheduleMainDAO scheduleMainDAO;
	private IQbcDao qbcDao;



	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public IScheduleMainDAO getScheduleMainDAO() {
		return scheduleMainDAO;
	}

	public void setScheduleMainDAO(IScheduleMainDAO scheduleMainDAO) {
		this.scheduleMainDAO = scheduleMainDAO;
	}

	/**
	 *
	 * 
	 * @param MeetingRoomEmploy
	 * @return
	 */
	public void save(String content, int year, int month, int day, int hour,
			int userid) {
		ScheduleMain main = new ScheduleMain();
		if (content != null && !"".equals(content)) {
			content = content.replace("\r\n", "");
			content = content.replace("?", "");
		}
		main.setPeopleId(userid);
		main.setScheduleYear(year);
		main.setScheduleMonth(month);
		main.setScheduleDay(day);
		main.setScheduleHour(hour);
		try {
			List list = scheduleMainDAO.findByExample(main);
			main.setContent(content);
			if (list.size() > 0) {
				ScheduleMain main2 = (ScheduleMain) list.iterator().next();
				main.setId(main2.getId());
				scheduleMainDAO.update(main);
			} else {
				scheduleMainDAO.save(main);
			}
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void saveWeek(String content,int hour, String showDate, int userid) {
		ScheduleMain main = new ScheduleMain();
		
		String[] split = showDate.split("-");
		main.setPeopleId(userid);
		if (content != null && !"".equals(content)) {
			content = content.replace("\r\n", "");
			content = content.replace("?", "");
			
		}
	
		main.setScheduleYear(Integer.valueOf(split[0]));
		main.setScheduleMonth(Integer.valueOf(split[1]));
		main.setScheduleDay(Integer.valueOf(split[2]));
		main.setScheduleHour(hour);
		try {
			List list = scheduleMainDAO.findByExample(main);
			
			if (list.size() > 0) {
				ScheduleMain main2 = (ScheduleMain) list.iterator().next();
				main.setContent(content);
				main.setId(main2.getId());
				scheduleMainDAO.update(main);
			} else {
				main.setContent(content);
				scheduleMainDAO.save(main);
			}
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void DeleteById(String content, int year, int month,
			int day, int scheduleHour, int userid) {
		ScheduleMain scheduleMain = new ScheduleMain();
		scheduleMain.setScheduleYear(year);
		scheduleMain.setScheduleMonth(month);
		scheduleMain.setScheduleDay(day);
		scheduleMain.setScheduleHour(scheduleHour);
		scheduleMain.setPeopleId(userid);
		List findByExample = scheduleMainDAO.findByExample(scheduleMain);
		if (findByExample.size() > 0) {
//			scheduleMain.setContent(content);
			ScheduleMain main = (ScheduleMain) findByExample.iterator().next();
			scheduleMain.setId(main.getId());
			scheduleMainDAO.delete(scheduleMain);
		}
	}

	public void DeleteByIdshowDate(String content,String showDate,int userid) {
		ScheduleMain scheduleMain = new ScheduleMain();
		String[] split = showDate.split("-");
		scheduleMain.setPeopleId(userid);
		
		scheduleMain.setScheduleYear(Integer.valueOf(split[0]));
		scheduleMain.setScheduleMonth(Integer.valueOf(split[1]));
		scheduleMain.setScheduleDay(Integer.valueOf(split[2]));
		scheduleMain.setPeopleId(userid);
		List findByExample = scheduleMainDAO.findByExample(scheduleMain);
		if (findByExample.size() > 0) {
//			scheduleMain.setContent(content);
			ScheduleMain main = (ScheduleMain) findByExample.iterator().next();
			scheduleMainDAO.delete(main);
		}
	}
	public List findByDate(ScheduleMain main) {
		LogicalExpression a = Restrictions.and(Restrictions.eq("peopleId", main
				.getPeopleId()), Restrictions.eq("scheduleYear", main
				.getScheduleYear()));
		a = Restrictions.and(a, Restrictions.eq("scheduleMonth", main
				.getScheduleMonth()));
		a = Restrictions.and(a, Restrictions.eq("scheduleDay", main
				.getScheduleDay()));
		DetachedCriteria criteria = DetachedCriteria
				.forClass(ScheduleMain.class);
		criteria.add(a);
		criteria.addOrder(Order.asc("scheduleHour"));
		List<ScheduleMain> list = qbcDao.getCriteria(criteria);
		
		List<ScheduleMain> scheduleMainList = new ArrayList<ScheduleMain>();
		for (ScheduleMain scheduleMain : list) {
			String temp =  scheduleMain.getContent().replace("?", "");
			scheduleMain.setContent(temp);
			scheduleMainList.add(scheduleMain);
		}
		
		return scheduleMainList;
	}
}
